package com.xupt.easy.link;

/**
 * @author zhaoxin
 * @title: ReverseList
 * @projectName LeetCode
 * @description: 反转链表
 * @date 2019/11/21 14:11
 */
public class ReverseList206 {

    /**
     * 反转一个单链表。
     *
     * 示例:
     *
     * 输入: 1->2->3->4->5->NULL
     * 输出: 5->4->3->2->1->NULL
     * @param head
     * @return
     */
    public ListNode reverseList(ListNode head) {
        // 迭代法
        ListNode prev = null;
        ListNode curr = head;

        while (curr != null){
            ListNode nodeTemp = curr.next;
            curr.next = prev;
            prev = curr;
            curr = nodeTemp;
        }

        return prev;
    }


    public static void main(String[] args) {
        ReverseList206 reverseList206 = new ReverseList206();
        ListNode listNode0 = new ListNode(0);
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode4 = new ListNode(4);
        ListNode listNode5 = new ListNode(5);

        listNode0.next = listNode1;
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;


        reverseList206.reverseList(listNode0);

    }


}
